﻿using System.Data;
using System.Linq;
using System.Web.Mvc;
using Coticula.Web.Models;
using Coticula.Web.Helpers;

namespace Coticula.Web.Controllers
{
    public class ProblemController : Controller
    {
        private readonly CoticulaDbContext _db = new CoticulaDbContext();

        private const int DefaultPageSize = 20;

        public PagedList<Problems> GetPagedProblems(int page, int take)
        {
            int skip = (page - 1) * take;
            var query = _db.Problems.OrderBy(r => r.Id);
            int resultCount = query.Count();

            var results = query.Skip(skip).Take(take).ToList();

            return new PagedList<Problems>()
            {
                Entities = results,
                HasMore = (skip + take < resultCount),
                HasPrevious = (skip > 0),
                CurrentPage = page
            };
        }

        //
        // GET: /Problem/
        // GET: /Problem/Index
        // GET: /Problem/Index/7
        // GET: /Problem/Index/7/20

        public ViewResult Index(int? page)
        {
            if (page == null) page = 1;
            const int pageSize = DefaultPageSize;

            ViewBag.Page = page;
            ViewBag.PageSize = pageSize;

            var pagedProblems = GetPagedProblems((int)page, pageSize);
            var results = pagedProblems.Entities;

            ViewBag.HasPrevious = pagedProblems.HasPrevious;
            ViewBag.HasMore = pagedProblems.HasMore;
            ViewBag.CurrentPage = pagedProblems.CurrentPage;

            return View(results);
        }

        //
        // GET: /Problem/Details/5

        public ViewResult Details(int id)
        {
            Problems problems = _db.Problems.Find(id);
            return View(problems);
        }

        //
        // GET: /Problem/Create

        [Authorize(Roles = "Administrator")]
        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Problem/Create

        [HttpPost]
        [Authorize(Roles = "Administrator")]
        public ActionResult Create(Problems problems)
        {
            if (ModelState.IsValid)
            {
                _db.Problems.Add(problems);
                _db.SaveChanges();
                return RedirectToAction("Details", new { id = problems.Id });
            }

            return View(problems);
        }

        //
        // GET: /Problem/Edit/5

        [Authorize(Roles = "Administrator")]
        public ActionResult Edit(int id)
        {
            Problems problems = _db.Problems.Find(id);
            return View(problems);
        }

        //
        // POST: /Problem/Edit/5

        [HttpPost]
        [Authorize(Roles = "Administrator")]
        public ActionResult Edit(Problems problems)
        {
            if (ModelState.IsValid)
            {
                _db.Entry(problems).State = EntityState.Modified;
                _db.SaveChanges();
                return RedirectToAction("Details", new { id = problems.Id });
            }
            return View(problems);
        }

        //
        // GET: /Problem/Delete/5

        [Authorize(Roles = "Administrator")]
        public ActionResult Delete(int id)
        {
            Problems problems = _db.Problems.Find(id);
            return View(problems);
        }

        //
        // POST: /Problem/Delete/5

        [HttpPost]
        [ActionName("Delete")]
        [Authorize(Roles = "Administrator")]
        public ActionResult DeleteConfirmed(int id)
        {
            Problems problems = _db.Problems.Find(id);
            _db.Problems.Remove(problems);
            _db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            _db.Dispose();
            base.Dispose(disposing);
        }
    }
}
